﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
namespace XmlTestProject
{
    public partial class Form1 : Form
    {
        public String filepath = "test.xml";
        private String path;
        private xmlHandler myXmlHandler = null;
        public Form1()
        {
            InitializeComponent();
            toolStripProgressBar1.Step = 1;
            toolStripProgressBar1.Maximum = 100;
            toolStripProgressBar1.Minimum = 0;
            path = filepath;
            myXmlHandler = new xmlHandler();

        }

        private void toolStripProgressBar1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int cycleSleepTime = 100;
            for (int i = 0; i < 100; i++)
            {
                toolStripProgressBar1.Increment(toolStripProgressBar1.Step);
                System.Threading.Thread.Sleep(cycleSleepTime);

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Create
            //XmlTextWriter class is used to create an Xml file. In this, 
            //we just specify the filepath and type of xml encoding.after that, we have to create a root element of xml document.
            //Ex: We are going to create an xml file with CustomerDetails as root element

            myXmlHandler.createNewXmlFile(filepath, "FinalProjectDataBase");

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Write- [insert into database]
            //We can add data into an xml file by just adding child nodes to the root element. In this, we just create some
            // xml elements and append those elements to the root as a child element and save the xml document.
            //Before writing, one thing is very important. All the data stored into the xml 
            //file will be treated as String only. So we must convert all data types to string.
            // Ex: We just add a customer "abc" with address as "xyz,india"

            XmlDocument xd = new XmlDocument();     //object creation for XmlDocument class
            FileStream lfile = new FileStream(filepath, FileMode.Open); //open an xml file using file stream
            xd.Load(lfile); //load opened file in xml document
            XmlElement cl = xd.CreateElement("Customer"); //create an xml element
            cl.SetAttribute("Name", "abc"); //set attribute for that element
            XmlElement na = xd.CreateElement("Address"); //create an xml element
            XmlText natext = xd.CreateTextNode("xyz,india"); //create text for xml element
            na.AppendChild(natext); //append address text to address node
            cl.AppendChild(na); //append address node to root element
            xd.DocumentElement.AppendChild(cl); //append root element to xml document
            lfile.Close(); //close the file stream
            xd.Save(filepath); //save the xmldocument content to the file
            String groupName = xmlInputDataTextBox.Text;
            
            if (groupName == "") 
                groupName = "BLANK" + (new Random()).Next();
            myXmlHandler.addGroupToDataBase(filepath, groupName);
        }

        private void editXmlbutton2_Click(object sender, EventArgs e)
        {
    // Update- [Updation of field]
    //Updation of data is the simplest process.we just read the required data and modify its content and save it.
    //we just retrieve a field (element) and change its InnerText or Set Its Attributes and Save the xml file.
    //Ex: Here we change the customer name from "abc" to "efgh" and change his address from "xyz,india" to "pqrs,india".
            XmlDocument xdoc = new XmlDocument();//creation of XmlDocument class Instance
            FileStream up = new FileStream(filepath, FileMode.Open);//create a file stream and open the file to be updated
            xdoc.Load(up);//load file into xmldocument instance
            XmlNodeList list = xdoc.GetElementsByTagName("Customer"); //find out the no of elements available in xml file
            for (int i = 0; i < list.Count; i++)//navigate through each and every nodes
            {
                XmlElement cu = (XmlElement)xdoc.GetElementsByTagName("Customer")[i];//retrieve the customer node
                XmlElement add = (XmlElement)xdoc.GetElementsByTagName("Address")[i];//retrieve the addresss node
                if (cu.GetAttribute("Name") == "abc")//compare it with node to be changed
                {
                    cu.SetAttribute("Name", "efgh");//set new attribute value
                    add.InnerText = "pqrs,india";//set new innertext of node
                    break;//break and get away from the for loop
                }
            }
            up.Close(); //close the file stream
            xdoc.Save(filepath); //save the xml file 
        }
        private void removeFromXmlbutton2_Click(object sender, EventArgs e)
        {
        //Remove
        //Removing process is also a simple one. We just navigate
        //through all nodes and finds node needs to be removed and remove it. We just remove the child element from the root element and save it
        //Ex: We just remove the customer named as "efgh".
            FileStream rfile = new FileStream(filepath, FileMode.Open); //open a file through file stream
            XmlDocument tdoc = new XmlDocument(); //create Xml Document Instance
            tdoc.Load(rfile); //load file
            XmlNodeList list = tdoc.GetElementsByTagName("Customer"); //find no of elements available in XmlDocument
            for (int i = 0; i < list.Count; i++) //Iterate through each element
            {
                XmlElement cl = (XmlElement)tdoc.GetElementsByTagName("Customer")[i];//get customer element
                if (cl.GetAttribute("Name") == "efgh") //Compare with element to be removed
                {
                    tdoc.DocumentElement.RemoveChild(cl); //Remove element from document
                }
            }
            rfile.Close(); //close stream
            tdoc.Save(filepath); //save file
        }

        private void readXmlbutton2_Click(object sender, EventArgs e)
        {
          //  Read-[fetch data]
//We can retrieve data from an xml file using XmlDocument class.here we just retrieve data based on the name of the element.we fetch data as whole and take what we want by navigating through its contents.
 //This reading will return everything as String only.After reading we must convert the data into its original datatype.
 //Ex: Here we just read the address of the customer named as "abc"

            XmlDocument xdoc = new XmlDocument(); //creation of XmlDocument class Instance
            FileStream rfile = new FileStream(path, FileMode.Open); //create a file stream and open the file to be updated
            xdoc.Load(rfile);//load file into xmldocument instance
            String address=""; //create a string variable to store address
            XmlNodeList list = xdoc.GetElementsByTagName("Customer"); //find out the no of elements available in xml file   
            for (int i = 0; i < list.Count; i++) //navigate through each and every nodes
            {
                XmlElement cl = (XmlElement)xdoc.GetElementsByTagName("Customer")[i]; //retrieve the customer node
                XmlElement add = (XmlElement)xdoc.GetElementsByTagName("Address")[i]; //retrieve the addresss node
                if ((cl.GetAttribute("Name")) == "abc") //compare it with node to be read
                {
                    address = add.InnerText; //assign address as innertext of node
                    break; //break and get away from the for loop
                }
            }
            rfile.Close(); //close the file stream
            
            Console.WriteLine("Read stuff: " + address);
            String[] results = myXmlHandler.fetchGroupsFromDataBase(filepath);
            String concated = "";
            foreach (String Val in results)
                concated+=Val + " ";
            xmlOutputLabel.Text = "FetchGroupMethod " + concated;
            
           
        }
    }
}
